#ifndef SCROBBLER_H
#define SCROBBLER_H

#include<QBuffer>
#include<QDir>
#include<QObject>
#include<QTimer>
#include<QMessageBox>
#include<QNetworkAccessManager>
#include<QNetworkRequest>
#include<QNetworkReply>
#include<QPointer>

#include<time.h>

#include"Player.h"

class Song;
class LFMCrypter;
class LFMParser;
class LFMPostDataBuilder;

#define SCROBBLING_ENABLED	true

class Scrobbler: public QObject{
Q_OBJECT
public:
	Scrobbler(QObject *parent);
	~Scrobbler();
	
	void getAuthenticationToken();
	
	void startSession();
	void stopSession();
	bool isActive()const;
	
public slots:
	void slotNowPlaying(QPointer<Song> song);
//	void slotPlayerStateChanged(Player::State state);
private slots:
	void slotSettingsChanged();
	void slotSendNowPlaying();
	void slotSendScrobble();
	void slotReplyReadyRead();
	void slotCurrentSongDeleted();
	void slotReplyReadyReadCorrection();
	void slotReplyReadyReadToken();
	void slotReplyReadyReadSession();
private:
	QTimer *m_timerNowPlaying;
	QTimer *m_timerScrobble;
	QPointer<Song> m_nowPlaying;
	time_t m_startedAt;
	time_t m_reallyStartedAt;
	qint64 m_timeTillScrobbleRemaining;
	QNetworkAccessManager *m_networkAccessManager;
	QNetworkReply *m_networkReply;
	QBuffer *m_buffer;
	LFMCrypter *m_crypter;
	LFMPostDataBuilder *m_postDataBuilder;
	
	//WARNING: This function will block until the appropriate user interaction
	bool userAuthentication(const QString &token);
	
	static const short int DELAY_NOW_PLAYING = 10000;
	
	QString readSessionKey();
	void writeSessionKey(const QString &sessionKey);
	static qint64 scrobbleDelay(QPointer<Song> song);
	//Appears to not be very interesting.
	//void getTrackInfo();
};

#endif
